<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>医生管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 操作区域 -->
    <div style="margin: 0 0 20px 0;">

      <el-button type="primary" size="medium" style="margin-left:20px;"
                 @click="$router.push('/service/doctor/add')">添加医生
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="departmentId" label="科室id"  align="center"
                       :show-overflow-tooltip="true"></el-table-column>

      <el-table-column prop="name" label="姓名"  align="center"
                       :show-overflow-tooltip="true"></el-table-column>

      <el-table-column prop="birth" label="生日"  align="center"
                       :show-overflow-tooltip="true"></el-table-column>

      <el-table-column prop="age" label="年龄" align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="phone" label="电话"  align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="professionalTitle" label="医生职称"  align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="resume" label="履历"  align="center"
                       :show-overflow-tooltip="true"></el-table-column>

      <el-table-column label="操作"  align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页控件 -->
    <div style="margin: 10px 0; text-align: right;">
      <el-pagination
          @current-change="changePage"
          :hide-on-single-page="true"
          :page-size="pageSize"
          :current-page="currentPage"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>

    <!-- 修改诊断的弹出表单Dialog -->
    <el-dialog title="修改医生" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="科室id" :label-width="formLabelWidth">
          <el-input v-model="editForm.departmentId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="生日" :label-width="formLabelWidth">
          <el-date-picker
              v-model="editForm.birth"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth">
          <el-input v-model="editForm.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="editForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="医生职称" :label-width="formLabelWidth">
          <el-input v-model="editForm.professionalTitle" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="履历" :label-width="formLabelWidth">
          <el-input v-model="editForm.resume" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表格数据
      tableData: [],
      // 分页相关数据
      currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
      pageSize: 20,
      pageCount: 1,
      total: 0,
      // 编辑对话框的可见性
      dialogFormVisible: false,
      // 编辑表单
      editForm: {
        departmentId: '',
        name: '',
        birth: '',
        age: '',
        phone: '',
        professionalTitle: '',
        resume:""
      },
      // 编辑表单的label宽度
      formLabelWidth: '100px'
    }
  },
  methods: {
// 打开编辑对话框
    openEditDialog(doctor) {
      let url = 'http://localhost:9999/doctor/' + doctor.id;
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.dialogFormVisible = true;
          this.editForm = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadRecordList();
            }
          });
        }
      });
    },
    // 提交编辑表单
    submitEditForm() {
      let url = 'http://localhost:9999/doctor/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      console.log(this.editForm.birth);
      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改诊断成功！',
            type: 'success'
          });
          this.loadRecordList();
          this.dialogFormVisible = false;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadRecordList();
            }
          });
        } else {
          this.$message.error(jsonResult.message);
        }
      });
    },
    // 打开删除确认框
    openDeleteConfirm(doctor) {
      let message = '此操作将永久删除该诊断，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(doctor);
      }).catch(() => {
      });
    },
    // 处理删除
    handleDelete(doctor) {
      console.log(doctor);
      let url = 'http://localhost:9999/doctor/' + doctor.id + '/delete';
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除诊断成功！',
            type: 'success'
          });
          this.loadRecordList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadRecordList();
            }
          });
        } else {
          this.$message.error(jsonResult.message);
        }
      });
    },
    // 切换分页
    changePage(page) {
      this.$router.replace('?page=' + page);
      this.loadRecordList();
    },
    // 加载相册列表
    loadRecordList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = 'http://localhost:9999/doctor?page=' + page;
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.pageCount = jsonResult.data.maxPage;
          this.total = jsonResult.data.total;
        }
      });
    }
  },
  mounted() {
    this.loadRecordList();
  }
}
</script>

<style scoped>
</style>